package cn.mw.cmdb.param;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.mw.cmdb.entity.AssetsDiscoverInfo;
import cn.mw.microMonitorCommon.api.scan.RuleParam;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class AssetsDiscoverRuleParam extends RuleParam {
    // 前端用ID确定属性唯一值
    private String id;
    private String protoType;
    private String version;


    /**
     * 资产发现规则参数转为扫描规则参数类
     * @param mwScanRuleTypes 资产发现规则集合
     * @return 扫描规则集合
     */
    public static List<RuleParam> discoverRuleConvertRuleParam(List<AssetsDiscoverRuleParam> mwScanRuleTypes , AssetsDiscoverInfo assetsDiscoverInfo) {
        List<RuleParam> ruleParams = new ArrayList<>();
        for (AssetsDiscoverRuleParam scanRule : mwScanRuleTypes) {
            RuleParam ruleParam = new RuleParam();
            BeanUtil.copyProperties(scanRule, ruleParam);
            // 表示为ICMP 和agent
            if (StrUtil.isBlank(scanRule.getVersion())) {
                ruleParam.setRuleType(scanRule.getProtoType());
            } else {
                ruleParam.setRuleType(scanRule.getVersion());
            }

            if(null != assetsDiscoverInfo.getReqTimeout()){
                ruleParam.setReqTimeout(assetsDiscoverInfo.getReqTimeout());
            }

            if(null != assetsDiscoverInfo.getReqRetryNum()){
                ruleParam.setReqRetryNum(assetsDiscoverInfo.getReqRetryNum());
            }
            ruleParams.add(ruleParam);
        }
        return ruleParams;
    }
}
